package com.lottery.controller;

import com.alibaba.fastjson.JSON;
import com.lottery.dto.AppPingOutputDTO;
import com.lottery.dto.SimpleOutputDTO;
import com.lottery.pojo.TableRegions;
import com.lottery.service.RegionService;
import com.lottery.utils.DateUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@SuppressWarnings("all")
@Slf4j
@RestController
public class RegionController {

    @Autowired
    private RegionService regionService;

    @PostMapping("getRegions")
    public List<TableRegions> getRegions() {
        return regionService.getRegions();
    }

    @PostMapping("updateRegion")
    public void updateRegion(@RequestBody List<TableRegions> tableRegions) {
        for (TableRegions region : tableRegions) {
            region.setUpdated_at(DateUtils.getSub4DT());
            log.info("updateRegion(), region:" + region.toString());
            regionService.updateRegion(region);
        }
    }

    @PostMapping("updateRegionLimit")
    public void updateRegionLimit(@RequestBody TableRegions region) {
        region.setUpdated_at(DateUtils.getSub4DT());
        log.info("updateRegionLimit(), region:" + region.toString());
        regionService.updateRegionLimit(region);
    }


    //--------------app
    @PostMapping("appPing")
    @SneakyThrows
    public AppPingOutputDTO appPing(String machine_number, String password, String time) {
        log.info("appPing()==>machine_number:{}, password:{}", machine_number, password);

        AppPingOutputDTO outputDTO = new AppPingOutputDTO();

        String urlAppLogin = "http://localhost:8001/appLogin?machine_number=" + machine_number + "&password=" + password;
        ResponseEntity<SimpleOutputDTO> appLogin = new RestTemplate().postForEntity(urlAppLogin, null, SimpleOutputDTO.class);
        log.info("appPing(), appLogin:{}", JSON.toJSON(appLogin.getBody().getData()));
        if ((Boolean) appLogin.getBody().getData()) {//app帐号密码校验成功
            outputDTO.setIsLogin(1);

            //今天星期几
            int weekDay = DateUtils.getWeekOfDate(new Date());

            List<TableRegions> regions = regionService.getRegions();
            if (regions == null || regions.size() < 1) {
                outputDTO.setIsRegionSuccess(-1);
                return outputDTO;
            }
            outputDTO.setIsRegionSuccess(1);
            String nowTime;
            if (time == null) {
                nowTime = DateUtils.nowTime();
            } else {
                nowTime = time;
            }
            SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
            switch (weekDay) {
                case 1:
                    for (TableRegions region : regions) {
                        switch (region.getRegion_key()) {
                            case "K":
                                boolean effectiveDateK = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart0()), timeFormat.parse(region.getEnd0()));
                                outputDTO.setRegionKEnable(effectiveDateK && region.getDisable0() == 0);
                                break;
                            case "F":
                                boolean effectiveDateF = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart0()), timeFormat.parse(region.getEnd0()));
                                outputDTO.setRegionFEnable(effectiveDateF && region.getDisable0() == 0);
                                break;
                            case "D":
                                boolean effectiveDateD = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart0()), timeFormat.parse(region.getEnd0()));
                                outputDTO.setRegionDEnable(effectiveDateD && region.getDisable0() == 0);
                                break;
                            case "S":
                                boolean effectiveDateS = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart0()), timeFormat.parse(region.getEnd0()));
                                outputDTO.setRegionSEnable(effectiveDateS && region.getDisable0() == 0);
                                break;
                        }
                    }
                    break;
                case 2:
                    for (TableRegions region : regions) {
                        switch (region.getRegion_key()) {
                            case "K":
                                boolean effectiveDateK = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart1()), timeFormat.parse(region.getEnd1()));
                                outputDTO.setRegionKEnable(effectiveDateK && region.getDisable1() == 0);
                                break;
                            case "F":
                                boolean effectiveDateF = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart1()), timeFormat.parse(region.getEnd1()));
                                outputDTO.setRegionFEnable(effectiveDateF && region.getDisable1() == 0);
                                break;
                            case "D":
                                boolean effectiveDateD = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart1()), timeFormat.parse(region.getEnd1()));
                                outputDTO.setRegionDEnable(effectiveDateD && region.getDisable1() == 0);
                                break;
                            case "S":
                                boolean effectiveDateS = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart1()), timeFormat.parse(region.getEnd1()));
                                outputDTO.setRegionSEnable(effectiveDateS && region.getDisable1() == 0);
                                break;
                        }
                    }
                    break;
                case 3:
                    for (TableRegions region : regions) {
                        switch (region.getRegion_key()) {
                            case "K":
                                boolean effectiveDateK = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart2()), timeFormat.parse(region.getEnd2()));
                                outputDTO.setRegionKEnable(effectiveDateK && region.getDisable2() == 0);
                                break;
                            case "F":
                                boolean effectiveDateF = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart2()), timeFormat.parse(region.getEnd2()));
                                outputDTO.setRegionFEnable(effectiveDateF && region.getDisable2() == 0);
                                break;
                            case "D":
                                boolean effectiveDateD = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart2()), timeFormat.parse(region.getEnd2()));
                                outputDTO.setRegionDEnable(effectiveDateD && region.getDisable2() == 0);
                                break;
                            case "S":
                                boolean effectiveDateS = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart2()), timeFormat.parse(region.getEnd2()));
                                outputDTO.setRegionSEnable(effectiveDateS && region.getDisable2() == 0);
                                break;
                        }
                    }
                    break;
                case 4:
                    for (TableRegions region : regions) {
                        switch (region.getRegion_key()) {
                            case "K":
                                boolean effectiveDateK = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart3()), timeFormat.parse(region.getEnd3()));
                                outputDTO.setRegionKEnable(effectiveDateK && region.getDisable3() == 0);
                                break;
                            case "F":
                                boolean effectiveDateF = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart3()), timeFormat.parse(region.getEnd3()));
                                outputDTO.setRegionFEnable(effectiveDateF && region.getDisable3() == 0);
                                break;
                            case "D":
                                boolean effectiveDateD = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart3()), timeFormat.parse(region.getEnd3()));
                                outputDTO.setRegionDEnable(effectiveDateD && region.getDisable3() == 0);
                                break;
                            case "S":
                                boolean effectiveDateS = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart3()), timeFormat.parse(region.getEnd3()));
                                outputDTO.setRegionSEnable(effectiveDateS && region.getDisable3() == 0);
                                break;
                        }
                    }
                    break;
                case 5:
                    for (TableRegions region : regions) {
                        switch (region.getRegion_key()) {
                            case "K":
                                boolean effectiveDateK = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart4()), timeFormat.parse(region.getEnd4()));
                                outputDTO.setRegionKEnable(effectiveDateK && region.getDisable2() == 0);
                                break;
                            case "F":
                                boolean effectiveDateF = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart4()), timeFormat.parse(region.getEnd4()));
                                outputDTO.setRegionFEnable(effectiveDateF && region.getDisable4() == 0);
                                break;
                            case "D":
                                boolean effectiveDateD = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart4()), timeFormat.parse(region.getEnd4()));
                                outputDTO.setRegionDEnable(effectiveDateD && region.getDisable4() == 0);
                                break;
                            case "S":
                                boolean effectiveDateS = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart4()), timeFormat.parse(region.getEnd4()));
                                outputDTO.setRegionSEnable(effectiveDateS && region.getDisable4() == 0);
                                break;
                        }
                    }
                    break;
                case 6:
                    for (TableRegions region : regions) {
                        switch (region.getRegion_key()) {
                            case "K":
                                boolean effectiveDateK = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart5()), timeFormat.parse(region.getEnd5()));
                                outputDTO.setRegionKEnable(effectiveDateK && region.getDisable5() == 0);
                                break;
                            case "F":
                                boolean effectiveDateF = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart5()), timeFormat.parse(region.getEnd5()));
                                outputDTO.setRegionFEnable(effectiveDateF && region.getDisable5() == 0);
                                break;
                            case "D":
                                boolean effectiveDateD = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart5()), timeFormat.parse(region.getEnd5()));
                                outputDTO.setRegionDEnable(effectiveDateD && region.getDisable5() == 0);
                                break;
                            case "S":
                                boolean effectiveDateS = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart5()), timeFormat.parse(region.getEnd5()));
                                outputDTO.setRegionSEnable(effectiveDateS && region.getDisable5() == 0);
                                break;
                        }
                    }
                    break;
                case 7:
                    for (TableRegions region : regions) {
                        switch (region.getRegion_key()) {
                            case "K":
                                boolean effectiveDateK = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart6()), timeFormat.parse(region.getEnd6()));
                                outputDTO.setRegionKEnable(effectiveDateK && region.getDisable6() == 0);
                                break;
                            case "F":
                                boolean effectiveDateF = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart6()), timeFormat.parse(region.getEnd6()));
                                outputDTO.setRegionFEnable(effectiveDateF && region.getDisable6() == 0);
                                break;
                            case "D":
                                boolean effectiveDateD = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart6()), timeFormat.parse(region.getEnd6()));
                                outputDTO.setRegionDEnable(effectiveDateD && region.getDisable6() == 0);
                                break;
                            case "S":
                                boolean effectiveDateS = DateUtils.isEffectiveDate(timeFormat.parse(nowTime), timeFormat.parse(region.getStart6()), timeFormat.parse(region.getEnd6()));
                                outputDTO.setRegionSEnable(effectiveDateS && region.getDisable6() == 0);
                                break;
                        }
                    }
                    break;
            }
            return outputDTO;
        }
        outputDTO.setIsLogin(-1);
        log.info("appLogin()<==OutputDTO:{}", outputDTO.toString());
        return outputDTO;
    }
}
